package com.itheima.mall.service.smsService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.mall.mapper.smsMapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.pojo.sms.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationServiceApi{

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;
    /**
     * 批量选择商品添加关联
     */
    @Override
    public void addFlashProductRelation(SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
    }
    /**
     * 删除商品关联
     */
    @Override
    public void deleteFlashProductRelation(Integer id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }
    /**
     * 分页查询不同场次关联及商品信息
     * flashPromotionId @param
     * flashPromotionSessionId  @param
     * 当前页码 @param pageNum
     * 当前页条目数 @param pageSize
     * @return
     */
    @Override
    public List<SmsFlashPromotionProductRelation> list(IPage page, Integer flashPromotionId, Integer flashPromotionSessionId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw =new LambdaQueryWrapper<>();
        lqw.eq(flashPromotionId != null,SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        lqw.eq(flashPromotionSessionId  != null,SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        lqw.orderByDesc(SmsFlashPromotionProductRelation::getSort);
        IPage iPage = smsFlashPromotionProductRelationMapper.selectPage(page, lqw);
        List records = iPage.getRecords();
        return records;
    }

    /**
     * 修改关联信息
     * @param id
     * @param smsFlashPromotionProductRelation
     * @return
     */
    @Override
    public void update(Long id, SmsFlashPromotionProductRelation smsFlashPromotionProductRelation) {
        SmsFlashPromotionProductRelation productRelation = smsFlashPromotionProductRelationMapper.selectById(id);
        if(productRelation==null){
            smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
        }else{
            smsFlashPromotionProductRelationMapper.updateById(smsFlashPromotionProductRelation);
        }
    }

    /**
     * 获取管理商品促销信息
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation selectProductionMessage(Long id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = smsFlashPromotionProductRelationMapper.selectById(id);
        return smsFlashPromotionProductRelation;
    }

    //根据id查询
    @Override
    public List<SmsFlashPromotionProductRelation> selectList(Long flashPromotionId) {
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        List<SmsFlashPromotionProductRelation> list = smsFlashPromotionProductRelationMapper.selectList(lqw);
        return list;
    }

    //获取商品数
    @Override
    public Integer getProductCount(Long flashPromotionId, Long id) {
        return smsFlashPromotionProductRelationMapper.getProductCount(flashPromotionId,id);
    }
}
